package suggestions

import (
	"github.com/gin-gonic/gin"
	"savor_gin/controllers"
	. "savor_gin/errorType"
	"savor_gin/models"
	"strconv"
	"time"
)

// 提交建议
func AddSuggestion(c *gin.Context) {
	var suggestion models.Suggestions
	if err := c.ShouldBindJSON(&suggestion); err != nil {
		controllers.SendResp(c, ParamBindError, nil)
		return
	}
	suggestion.SuggestionDate = time.Now()
	if err := suggestion.Create(); err != nil {
		controllers.SendResp(c, DataBaseError, nil)
		return
	}
	controllers.SendResp(c, OK, suggestion)
}

// 删除建议
func DeleteSuggestion(c *gin.Context) {
	suggestionId, err := strconv.Atoi(c.Query("suggestionId"))
	if err != nil {
		controllers.SendResp(c, ParamBindError, err)
		return
	}
	s := &models.Suggestions{SuggestionID: uint64(suggestionId)}
	if err := s.Delete(); err != nil {
		controllers.SendResp(c, DataBaseError, err)
		return
	}
	controllers.SendResp(c, OK, nil)
}

// 查询个人建议
func GetSuggestionListByUserId(c *gin.Context) {
	userId, err := strconv.Atoi(c.Query("userId"))
	if err != nil {
		controllers.SendResp(c, ParamBindError, err)
		return
	}
	if s, err := models.GetSuggestionByUserId(uint64(userId)); err != nil {
		controllers.SendResp(c, DataBaseError, err)
		return
	} else {
		controllers.SendResp(c, OK, s)
	}
}

// 查询食谱对应的建议
func GetSuggestionListByRecipeId(c *gin.Context) {
	recipeId, err := strconv.Atoi(c.Query("recipeId"))
	if err != nil {
		controllers.SendResp(c, ParamBindError, err)
		return
	}
	if s, err := models.GetSuggestionByRecipeId(uint64(recipeId)); err != nil {
		controllers.SendResp(c, DataBaseError, err)
		return
	} else {
		controllers.SendResp(c, OK, s)
	}
}
